<template>
  <div>
    <!-- 三级分类 -->
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0px">
      <!-- 场景 0-->
      <div v-show="scene == 0">
        <el-button
          @click="addSpu"
          type="primary"
          size="default"
          icon="Plus"
          :disabled="categroyStore.c3Id ? false : true"
          >添加SPU</el-button
        >
        <!-- 展示已有SPU数据 -->
        <el-table style="margin: 10px 0px" border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作">
            <!-- row:即为已有的SPU对象 -->
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="View"
                title="查看SKU列表"
                @click="findSku(row)"
              ></el-button>

              <el-popconfirm
                :title="`你确定删除${row.spuName}`"
                width="200px"
                @confirm="deleteSpu(row)"
              >
                <template #reference>
                  <el-button
                    type="primary"
                    size="small"
                    icon="Delete"
                    title="删除已有的SPU"
                  >
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :small="true"
          :disabled="false"
          :background="true"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getHasSpu"
          @size-change="changeSize"
        />
      </div>

      <!-- 添加SPU 和修改 SPU -->
      <SpuForm
        ref="spuRef"
        v-show="scene == 1"
        @changeScene="changeScene"
      ></SpuForm>

      <!-- 添加SKU -->
      <SkuForm
        ref="skuRef"
        v-show="scene == 2"
        @changeScene="changeScene"
      ></SkuForm>
      <!-- dialog 对话框,展示已有的SKU数据 -->
      <el-dialog v-model="show" title="SKU列表">
        <el-table border :data="skuArr">
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template #="{ row, $index }">
              <img
                :src="row.imgUrl"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reqHasSpu, reqSkuList, reqRemoveSpu } from "@/api/product/spu";
import type {
  HasSpuResponseData,
  Records,
  SpuData,
  SkuInfoData,
  SkuData,
} from "@/api/product/spu/type";
import useCategoryStore from "@/store/modules/category";
import { ref, watch, onBeforeMount } from "vue";
// 引入子组件
import SpuForm from "./spuForm.vue";
import SkuForm from "./skuForm.vue";
import { ElMessage } from "element-plus";

// 仓库
let categroyStore = useCategoryStore();
// 场景的数据
let scene = ref<number>(0);
// 分页器默认的页面
let pageNo = ref<number>(1);
// 每页展示几条数据
let pageSize = ref<number>(3);

// 存储已有SPU的数据
let records = ref<Records>([]);
// 储存已有SPU总个数
let total = ref<number>();
let spuRef = ref<any>();
// 获取子组件实列SkUForm
let skuRef = ref<any>();
// 存储全部的SKU数据
let skuArr = ref<SkuData[]>([]);

// 控制SKU列表的弹框的显示
let show = ref<boolean>(false);

// 监听三级分类ID变化
watch(
  () => categroyStore.c3Id,
  () => {
    // 务必保证有三级分类的ID
    if (!categroyStore.c3Id) return;
    getHasSpu();
  },
);

// 此方法执行,可以获取某一个三级分类下全部的已有SPU
const getHasSpu = async (pager = 1) => {
  // 修改当前页面
  pageNo.value = pager;
  let result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categroyStore.c3Id,
  );
  if (result.code == 200) {
    records.value = result.data.records;
    total.value = result.data.total;
  }
  console.log(result);
};

// 分页器下拉菜单发送变化的时候
const changeSize = () => {
  getHasSpu();
};

// 添加心的SPU按钮的回调
const addSpu = () => {
  // 切换为场景1:添加与修改已有SPU结构
  (scene.value = 1), spuRef.value.initAddSpu(categroyStore.c3Id);
};

// 子组件SpuForm 绑定的自定义事件
const changeScene = (obj: any) => {
  // 子组件SpuForm
  scene.value = obj.flag;
  // 再次获取全部的SPU
  if (obj.params == "update") {
    getHasSpu(pageNo.value);
  } else {
    getHasSpu();
  }
};

// 修改已有SPU的按钮的回调

const updateSpu = (row: SpuData) => {
  console.log(row);
  // 切换为场景1:添加与修改已有SPU结构
  scene.value = 1;

  // 调用子组件实列方法获取完整已有SPU的数据
  spuRef.value.initHasSpuData(row);
};

// 添加sku的回调函数
const addSku = (row: SpuData) => {
  // 点击添加按钮的时候切换场景为2
  scene.value = 2;
  // 调用子组件的方法初始化添加SKU的数据
  skuRef.value.initSkuData(categroyStore.c1Id, categroyStore.c2Id, row);
};

// 查看sku的列表
const findSku = async (row: SpuData) => {
  let result: SkuInfoData = await reqSkuList(row.id as number);
  if (result.code == 200) {
    skuArr.value = result.data;
    // 对话框显示出来
    show.value = true;
  }
  console.log(result, 111);
};

// 删除spu
const deleteSpu = async (row: SpuData) => {
  let result: any = await reqRemoveSpu(row.id as number);
  if (result.code == 200) {
    ElMessage({
      type: "success",
      message: "删除成功!",
    });
    //获取已有的SPU数据
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1);
  } else {
    ElMessage({
      type: "error",
      message: "删除失败!",
    });
  }
};

// 路由组件销毁前
onBeforeMount(() => {
  categroyStore.$reset();
});
</script>

<style scoped lang="scss"></style>
